import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useAuthStore } from "@/stores/auth.js";
import {ElMessage } from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path:'/login',
      name:'login',
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/register',
      name:'register',
      component: () => import('@/views/RegisterView.vue')
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutDeveloperView.vue')
    },
    {
      path: '/xhsDetail',
      name: 'xhsDetail',
      component: () => import('@/views/xhs/DetailPage.vue'),
      meta:{ requiresAuth: true}
    },
    {
      path: '/taskHub',
      name: 'taskHub',
      component: () => import('@/views//TaskHubView.vue')
    },
    {
      path:"/aboutDeveloper",
      name: "aboutDeveloper",
      component: () => import('@/views/AboutDeveloperView.vue')
    },
    {
      path:"/profile",
      name:"profile",
      component: () => import('@/views/PersonalPageVue.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
    const isAuthenticated = authStore.isAuthenticated
  if (to.meta.requiresAuth && !authStore.accessToken) {
    ElMessage.info("请先登陆!")
    next('/login')
  } else {
    next()
  }
})

export default router
